using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using RSSRadio.Model;

namespace RSSRadio.Model
{
	///<summary>
	///   - class object to store podcast info            
	///</summary>
	public class PodcastInfo : IComparable
	{
		[XmlAttribute("feedName")]
		public string FeedName;
        [XmlAttribute("feedUrl")]
        public string FeedUrl;



        [XmlElement(ElementName = "Details")]
        public EpisodeDetails Details;

        [XmlAttribute("filepath")]
		public string FilePath;
		///<summary>
		/// PodcastInfo constructor
		///</summary>
		public PodcastInfo()
		{
			FeedName = "";
            FeedUrl = "";
            Details = new EpisodeDetails();
			FilePath = "";
		}
		public PodcastInfo(EpisodeDetails episodeDetails, string FeedName, string FeedUrl,string FilePath)
		{
            this.Details = episodeDetails;
            this.FeedName = FeedName;
            this.FeedUrl = FeedUrl;
            this.FilePath = FilePath;
		}

		public string ToMobileHTML()
		{
			StringBuilder html = new StringBuilder("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD "
				+ "HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
				+ "<html><head><title>Episode Details</title><style type=\"text/css\">"
				+ "<!-- body { margin: 9px } --> </style></head><body>");

			html.Append("<strong>");
			html.Append(this.FeedName);
			html.Append("</strong><br />");
			html.Append(this.Details.Date);
			html.Append("<br /><br /><strong>");
			html.Append(this.Details.Title);
			html.Append("</strong><br /><br />");
			html.Append(this.Details.Description);
			html.Append("</body></html>");

			return html.ToString();
		}




		/// <summary>
		/// Converts the podcast publication date to a DateTime object
		/// </summary>
		/// <returns>DateTime.MinValue if unable to convert</returns>
		public DateTime PubDateToDateTime()
		{
			DateTime podDate = DateTime.MinValue;

			// the following code from RSS.net: RssReader.cs
			// http://www.rssdotnet.com/
			try
			{
				podDate = DateTime.Parse(this.Details.Date);
			}
			catch
			{
				try
				{
                    string tmp = this.Details.Date.ToString();
					tmp = tmp.Substring(0, tmp.Length - 5);
					tmp += "GMT";
					podDate = DateTime.Parse(tmp);
				}
				catch
				{
				}
			}
			// end RSS.net code

			return podDate;
        }

        #region Properties
        public Uri URL
        {
            get { return Details.Url; }
        }
        #endregion

        #region IComparable<PodcastInfo> Members


        /// <summary>
		/// provides a way to compare PodcastInfo objects. Copares poddcasts URLs and pubish dates.
		/// </summary>
		/// <param name="other"></param>
		/// <returns>0 if equal, 1 if greater than other, -1 if less than other</returns>
		public int CompareTo(object other)
		{
            int result = 1;
            if (other is PodcastInfo)
            {
                // sort 'backwards'
                result = ((PodcastInfo)other).PubDateToDateTime().CompareTo(this.PubDateToDateTime());

                if (result == 0)
                    result = this.Details.Title.CompareTo(((PodcastInfo)other).Details.Title);
            }
            
            return result;
		}

		#endregion

        public override bool Equals(Object obj)
        {
            bool result = false;
            // Check for null values and compare run-time types.
            if (obj != null && GetType() == obj.GetType())
            {
                result = (this.Details.Url.ToString() == (((PodcastInfo)obj).Details.Url.ToString()));

                if (result )
                    result = ((PodcastInfo)obj).PubDateToDateTime() == (this.PubDateToDateTime());

            }

            return result;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
